package com.itheima.sky.controller.backend;

import cn.hutool.core.util.IdUtil;
import com.itheima.sky.dto.SetmealDTO;
import com.itheima.sky.dto.SetmealSaveDTO;
import com.itheima.sky.entity.PageResult;
import com.itheima.sky.entity.Result;
import com.itheima.sky.service.SetmealService;
import com.itheima.sky.utils.UUIDUtils;
import com.itheima.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description DishController
 * @Author oumei
 * @Date 2024-06-27
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealControllerBack {

    @Autowired
    private SetmealService setmealService;

    /**
     * 查找套餐分页查询
     * @param setmealDto
     * @return
     */
    @GetMapping("/page")
    public Result  findSetmealAll(SetmealDTO setmealDto) {

        log.info("查找套餐分页查询：{}",setmealDto);

        PageResult<SetmealVO> setmealPageResult = setmealService.findSetmealPageAll(setmealDto);
        return Result.success(setmealPageResult);
    }

    /**
     * 新增套餐保存
     * @param setmealSaveDTO
     * @param session
     * @return
     */
    @PostMapping
    public Result saveSetmeal(@RequestBody SetmealSaveDTO setmealSaveDTO, HttpSession session) {

        //获取登录者信息补全信息
//        Long empId = (Long) session.getAttribute("employee");
        Long empId = 1L;
        //添加一个菜单套餐id(雪花算法)
        setmealSaveDTO.setId(IdUtil.createSnowflake(1,2).nextId());
        setmealSaveDTO.setCreateUser(empId);
        setmealSaveDTO.setUpdateUser(empId);
        setmealService.save(setmealSaveDTO);
        return Result.success("保存成功");
    }


    /**
     * 删除套餐信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        return setmealService.delete(ids);
    }

    /**
     * 批量起售成功
     * @param ids
     * @return
     */
    @PostMapping("status/1")
    public Result updatestatus1(@RequestParam List<Long> ids) {
        setmealService.updatestatus1(ids);
        return Result.success("批量起售成功");
    }

    @PostMapping("status/0")
    public Result updatestatus0(@RequestParam List<Long> ids) {
        setmealService.updatestatus0(ids);
        return Result.success("批量停售成功");
    }

    /**
     * 获取修改前的数据
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public Result findBefore(@PathVariable Long id) {

        log.info("修改前获取到原来的数据的参数：{}",id);

        return Result.success(setmealService.findBeforeById(id));
    }

    @PutMapping
    public Result update(@RequestBody SetmealSaveDTO setmealSaveDTO/*, HttpSession session*/) {

        //Long empId = (Long) session.setAttribute("employee");
        setmealSaveDTO.setUpdateUser(1L);
        setmealSaveDTO.setCreateUser(1L);
        setmealService.updateById(setmealSaveDTO);

        return Result.success("修改成功");
    }
}
